//
//  HFHomeViewController.swift
//  sinaMicroBlog
//
//  Created by mac on 15/11/8.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD

private let HomeCellID = "HomeCellID"

class HFHomeViewController: HFBaseTableViewController {
    
    private lazy var statues = [HFStatus]()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        if !userLogin {
            visitorLoginView?.setUIInfo(ImageName: nil, Title: "关注一些人，回这里看看有什么惊喜")
            return
        }
        
        prepareTableView()
        
        loadData()
        
    }
    
    private func prepareTableView() {
        
        refreshControl = UIRefreshControl()
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
        
        tableView.registerClass(HFStatusTableViewCell.self, forCellReuseIdentifier: HomeCellID)
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.separatorStyle = .None
        tableView.tableFooterView = indicatorView
    }
    
    @objc private func loadData() {
        var since_id = 0
        var max_id = 0
        if indicatorView.isAnimating() {
            //上拉加载更多数据
            since_id = 0
            max_id = statues.last?.id ?? 0
        } else {
            //下拉刷新
            max_id = 0
            since_id = statues.first?.id ?? 0
        }

        HFStatusListViewModel.loadData(since_id, max_id: max_id) { (array) -> () in
            self.refreshControl?.endRefreshing()
            if array == nil {
                SVProgressHUD.showErrorWithStatus(AppError)
                self.indicatorView.stopAnimating()
                return
            }
            if since_id > 0 {
                self.statues = array! + self.statues
            }else if max_id > 0 {
                self.statues += array!
                self.indicatorView.stopAnimating()
            } else {
                self.statues = array!
            }
            
            self.tableView.reloadData()
        }
    }
    private lazy var indicatorView: UIActivityIndicatorView = {
        let indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
        
        return indicator
    }()
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension HFHomeViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statues.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> HFStatusTableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellID, forIndexPath: indexPath) as! HFStatusTableViewCell
        cell.status = statues[indexPath.row]
        return cell
    }
    
}




